// 根据用户选择的周计算出周的结束日期
export function getDay(date) {
    var now = new Date(date);
    var nowTime = now.getTime();
    var day = now.getDay();
    var oneDayLong = 24 * 60 * 60 * 1000;
    var MondayTime = nowTime - (day - 1) * oneDayLong;
    var SundayTime = nowTime + (7 - day) * oneDayLong;
    var monday = new Date(MondayTime);
    var sunday = new Date(SundayTime);
    var timeStart = formatDateTime(monday);
    var timeEnd = formatDateTime(sunday);
    return [timeStart, timeEnd]
}

// 将国际时间格式转化为yyyymmdd
export function formatDateTime(time) {
    let year = time.getFullYear();
    let month = time.getMonth() + 1 < 10 ? "0" + (time.getMonth() + 1) : time.getMonth() + 1;
    let date = time.getDate() < 10 ? "0" + time.getDate() : time.getDate();
    return year + "" + month + "" + date;
}
// 日期转化yyyy-mm
export function dateToTimeShortDate(date) {
    let yyyy = date.getFullYear();
    let mth = dateNum2Str(date.getMonth() + 1);
    return `${yyyy}-${mth}`;
}

// 日期转化yyyy-mm-dd
export function timeToDate(date) {
    let yyyy = date.getFullYear();
    let mth = dateNum2Str(date.getMonth() + 1);
    let dd = dateNum2Str(date.getDate());
    return `${yyyy}-${mth}-${dd}`;
}

// 秒数转化为几分几秒
export function getMinutesString(seconds) {
    let string = '';
    if (seconds > 60) {
        var minutes = parseInt(seconds / 60);
        var second = seconds - (minutes * 60);
        string = minutes + '分' + second + '秒'
    } else {
        string = '0分' + seconds + '秒';
    }
    return string
}

//时间戳转化
export function dateToTimeStr(date) {
    let yyyy = date.getFullYear();
    let mth = dateNum2Str(date.getMonth() + 1);
    let dd = dateNum2Str(date.getDate());
    let hh = dateNum2Str(date.getHours());
    let mm = dateNum2Str(date.getMinutes());
    let ss = dateNum2Str(date.getSeconds());
    return `${yyyy}-${mth}-${dd} ${hh}:${mm}:${ss}`;
}

export function dateToShortTimeStr(date) {
    let yyyy = date.getFullYear();
    let mth = dateNum2Str(date.getMonth() + 1);
    let dd = dateNum2Str(date.getDate());
    let hh = dateNum2Str(date.getHours());
    let mm = dateNum2Str(date.getMinutes());
    let ss = dateNum2Str(date.getSeconds());
    return `${yyyy}${mth}${dd}${hh}${mm}${ss}`;
}


const ONE_DAY_MILLISECONDS = 24 * 60 * 60 * 1000; //一天的时间
// 计算时间差
export function computeDays(targetDateStr, fromDateStr) {
    if (targetDateStr) {
        let targetTime = timeStrToObj(targetDateStr);
        let fromDate;
        if (fromDateStr) {
            fromDate = timeStrToObj(fromDateStr)
        } else {
            fromDate = new Date
        }
        let diff = targetTime - fromDate;
        let days = diff / ONE_DAY_MILLISECONDS;
        if (days < 1 && days > 0) {
            return 1;
        } else if (days < 0) {
            return -1;
        } else {
            return Math.round(days);
        }
    }
    return 0
}

export function timeStrToObj(str) {
    let strArr = str.split(" ");
    if (strArr.length === 2) {
        let dateArr = strArr[0].split("-");
        let timeArr = strArr[1].split(":");

        let yyyy = dateArr[0];
        let mth = dateArr[1] - 1;
        let dd = dateArr[2];
        let hh = timeArr[0] || "00";
        let mm = timeArr[1] || "00";
        let ss = timeArr[2] || "00";
        return new Date(yyyy, mth, dd, hh, mm, ss);
    } else {
        return "";
    }
}

export function dateNum2Str(num) {
    if (num < 10) {
        return "0" + num;
    } else {
        return num + "";
    }
}
//多行省略 
export function ellipsisStr(str, number) {
    let num = number ? number : str.length
    if (num > str.length) {
        return str.substring(0, num)
    } else {
        return str.substring(0, num) + '...'
    }
}

// 拷贝
export function clone(obj) {
    return JSON.parse(JSON.stringify(obj));
}


//节流 方法2
export function throttle_2(fn, delay = 200) {
    let timer = null
    return function () {
        if (timer) return
        timer = setTimeout(() => {
            fn.apply(this, arguments)
            timer = null
        }, delay)
    }
}

//j节流方法 3 
export function throttle_3(fn, delay) {
    let timer = null
    let starttime = Date.now()
    return function () {
        let curTime = Date.now() //当前时间
        let remaining = delay - (curTime - starttime) //从上次一到现在，还剩下多少时间
        let that = this
        let args = arguments
        clearTimeout(timer)
        if (remaining <= 0) {
            fn.apply(that, args)
            starttime = Date.now()
        } else {
            timer = setTimeout(fn, remaining)
        }

    }
}

//防抖2 
export function deboince_1(fn, delay) {
    let timeout;
    return function () {
        let that = this;  //保存this 指向
        let args = arguments
        clearTimeout(timeout)
        timeout = setTimeout(() => {
            fn.apply(that, args)
        }, delay)
    }
}


